import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

// 注意:解决路由跳转问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
}

const routes = [
  {
    path: "/",
    redirect: "/index"
  },
  // 首页
  {
    path: "/index",
    name: "index",
    component: () => import('../views/indexView.vue'),
    meta: {
      keep: true,
    }
  },
  // 商品列表页
  {
    path: "/shop",
    name: "shop",
    component: () => import('../views/shopView.vue')
  },
  // 购物车页
  {
    path: "/car",
    name: "car",
    component: () => import('../views/carView.vue'),
    meta: {
      keep: false,
    },
    beforeEnter: (to, from, next) => {
      let user = localStorage.getItem("HL_user")
      if (!user) {
        next("/login");
        return;
      } else {
        next();
      }

    }
  },
  // 附近店铺页
  {
    path: "/shops",
    name: "shops",
    component: () => import('../views/shopsView.vue')
  },
  // 个人页
  {
    path: "/My",
    name: "My",
    component: () => import('../views/MyView.vue'),
    beforeEnter: (to, from, next) => {
      let user = localStorage.getItem("HL_user")
      if (!user) {
        next("/login");
        return;
      } else {
        next();
      }
    }
  },
  // 搜索页
  {
    path: "/search",
    name: "search",
    component: () => import('../views/searchView.vue'),
    meta: {
      keep: false,
    }
  },
  // 商品页
  {
    path: "/shopList",
    name: "shopList",
    component: () => import('../views/shopList.vue'),
    meta: {
      keep: false,
    }
  },
  // 详情页
  {
    path: "/details",
    name: "details",
    component: () => import('../views/detailsView.vue')
  },
  // 登录页
  {
    path: "/login",
    name: "login",
    component: () => import('../views/loginView.vue')
  },
  // 收藏页面
  {
    path: "/like",
    name: "like",
    component: () => import('../views/likeView.vue'),
  },
  // 地址页面
  {
    path: "/address",
    name: "address",
    component: () => import('../views/addressView.vue'),
  },
  // 编辑地址页面
  {
    path: "/changeAddress",
    name: "changeAddress",
    component: () => import('../views/changeAddress.vue'),
  },
  // 订单页面
  {
    path: "/order",
    name: "order",
    component: () => import('../views/orderView.vue'),
  },
  // 确认订单页面
  {
    path: "/confirmOrder",
    name: "confirmOrder",
    component: () => import('../views/confirmOrder.vue'),
  },
  // 优惠券页面
  {
    path: "/coupon",
    name: "coupon",
    component: () => import('../views/couponView.vue'),
  },

]



const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
  linkActiveClass: "con"
})

export default router
